package com.cdu.guoxinan.www.smartcom.controller.propertyController;

import com.cdu.guoxinan.www.smartcom.aop.Annotation.OperationLog;
import com.cdu.guoxinan.www.smartcom.domian.dto.ComplaintDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.QueryCompDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Complaint;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Resident;
import com.cdu.guoxinan.www.smartcom.service.ComplaintService;
import com.cdu.guoxinan.www.smartcom.service.ResidentService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.cdu.guoxinan.www.smartcom.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/comp")
@Api(tags = "投诉管理")
public class ComplaintController {
    @Resource
    private ComplaintService complaintService;

    @Resource
    private ResidentService residentService;


    @GetMapping("/all")
    @ApiModelProperty("获取所有投诉信息")
    @OperationLog(description = "获取所有投诉信息")
    public R getAllComplaints(QueryCompDTO queryCompDTO) {

        return R.ok(complaintService.getAllComplaints(queryCompDTO));
    }

    @PostMapping("/update")
    @ApiModelProperty("修改投诉信息")
    @OperationLog(description = "修改投诉信息")
    public R updateComplaint( Complaint complaint) {
        return complaintService.update(complaint)  ? R.ok("修改成功") : R.error("修改失败");
    }

    @PostMapping("/delete")
    @ApiModelProperty("删除投诉信息")
    @OperationLog(description = "删除投诉信息")
    public R deleteComplaint(@RequestParam Long complaintId) {
        return complaintService.delete(complaintId)  ? R.ok("删除成功") : R.error("删除失败");
    }

    @PostMapping("/add")
    @ApiModelProperty("添加投诉信息")
    @OperationLog(description = "添加投诉信息")
    public R addComplaint( @RequestBody ComplaintDTO complaintDTO) {

        System.out.println("complaintDTO = " + complaintDTO);
       Resident resident = residentService.findResidentByUserId(complaintDTO.getUserId());
        System.out.println("resident = " + resident);
        if (resident == null) {
            return R.error("用户不存在");
        }
        complaintDTO.setResidentId(resident.getResidentId());
        return complaintService.add(complaintDTO)? R.ok("投诉成功") : R.error("投诉失败");
    }

    @GetMapping("/getAllById")
    @ApiModelProperty("通过id获取投诉信息")
    @OperationLog(description = "通过id获取投诉信息")
    public R getAllById(@RequestParam Integer userId) {
        Resident resident = residentService.findResidentByUserId(userId);

        if ( null == resident){
            return R.error("用户不存在");
        }
        Long residentId = resident.getResidentId();
        return R.ok(complaintService.getAllById(residentId));
    }
}

